package edu.hawaii.halealohacli.command;

import java.io.IOException;
import org.wattdepot.client.WattDepotClient;

/**
 * Provides the user a help guide of commands.
 * 
 * @author Group BackR
 * 
 */
public class Help implements Command {

  private String command = "help";

  @Override
  public String run(String[] input, WattDepotClient client) {
    return getHelp();
  }

  @Override
  public String getCommand() {
    return command;
  }

  @Override
  public int getNumParams() {
    return 0;
  }

  @SuppressWarnings({ "rawtypes" })
  @Override
  public String getHelp() {
    StringBuffer output = new StringBuffer(196);
    // Construct new CommandManager.
    CommandManager manager = new CommandManager();
    // Get a list of all classes in the Command package.
    Class[] commandList;
    try {
      commandList = manager.getClasses();
      // Initialize a class array of parameters to search for the getCommand method.
      Class[] params = {};
      // Iterate through the commandList.
      for (Class tempClass : commandList) {
        // Get the name of the current package.
        String packageName = tempClass.getPackage().getName();
        // Get all command classes except the help class to prevent recursive loop.
        if (!tempClass.getName().equals(packageName + ".Help")) {
          // Get the help string from each command class and append to the buffer.
          output.append(manager.run(tempClass, "getHelp", params, null, null));
          output.append("\n\n");
        }
      }
    }
    catch (ClassNotFoundException e) {
      System.out.println("Could not find " + command + " command as a class.");
    }
    catch (IOException e) {
      System.out.println("An unexpected file error has occured. Please try again.");
    }
    // Create a command to exit the program.
    String quit = "quit \nTerminates execution.\n\n";
    // Append the quit command to the output.
    output.append(quit);
    // Create a user note for tips.
    String note1 = "Note: towers are:  Mokihana, Ilima, Lehua, Lokelani\n";
    String note2 = "Lounges are the tower names followed by a \"-\" followed by ";
    String note3 = "one of A, B, C, D, E. For example, Mokihana-A.";
    String note = note1 + note2 + note3;
    // Append the user note to the output.
    output.append(note);
    // Return the buffer as a string.
    return output.toString();
  }

}
